package com.demo.core.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.demo.core.dao.SiteDao;
import com.demo.core.model.Site;
import com.demo.core.service.base.SiteService;

/**
 * 站点服务实现类
 * 
 * @author ryuu.kk
 *
 */
public class SiteServiceImpl implements SiteService {
	/**
	 * 站点Dao
	 */
	private SiteDao siteDao;
	
	@Override
	@Transactional
	public Long save(Site site) {
		Long id = siteDao.insert(site);
		return id;
	}
	
	@Override
	public boolean exists(Long id) {
		return siteDao.exists(id);
	}
	
	@Override
	public int delete(Long siteId) {
		return siteDao.delete(siteId);
	}

	@Override
	public void update(Long siteId, Site site) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", siteId);
		siteDao.update(site, params);
	}
	
	@Override
	public int updateStatus(Long siteId, String status) {
		Site site = new Site();
		Map<String, Object> params = new HashMap<String, Object>(2);
		site.setStatus(status);
		params.put("id", siteId);
		return siteDao.updateStatus(site, params);
	}

	@Override
	public int updateOwner(Long siteId, Site site) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", siteId);
		return siteDao.updateOwner(site, params);
	}

	@Override
	public int updateSuffix(Long siteId, String suffix) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", siteId);
		Site site = new Site();
		site.setSuffix(suffix);
		return siteDao.updateSuffix(site, params);
	}
	
	@Override
	public void close(Long siteId) {
		close(siteId, null);
	}

	@Override
	public void close(Long siteId, String defaultUrl) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", siteId);
		Site site = new Site();
		site.setIsClose(true);
		site.setDefaultUrl(defaultUrl);
		siteDao.close(site, params);
	}
	
	@Override
	public void open(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", siteId);
		Site site = new Site();
		site.setIsClose(false);
		siteDao.close(site, params);
	}
	
	@Override
	public Site find(Long siteId) {
		return siteDao.select(siteId);
	}

	@Override
	public List<Site> findAll(Long siteGroupId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("siteGroupId", siteGroupId);
		return siteDao.select(params);
	}
	@Override
	public List<Site> findAll() {
		return siteDao.select(new HashMap<String, Object>(1));
	}
	public void setSiteDao(SiteDao siteDao) {
		this.siteDao = siteDao;
	}
	@Override
	public Site findByDomain(String domain) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("domain", domain);
		List<Site> sites = siteDao.select(params);
		return sites.size() > 0 ? sites.get(0) : null;
	}
}
